<?php
Yii::import('bootstrap.widgets.TbCKEditor');

class YCKEditor extends TbCKEditor
{
	/**
	 * 允许文件上传
	 *
	 * @var bool
	 */
	public $allowUpload = true;

	/**
	 * 允许文件浏览
	 *
	 * @var bool
	 */
	public $allowBrowse = true;

	public function init()
	{
		parent::init();
		if (!isset($this->form) && $this->owner instanceof CActiveForm) {
			$this->form = $this->owner;
		}

		if (!isset($this->editorOptions['toolbarGroups'])) {
			$this->editorOptions['toolbarGroups'] = $this->getStandardToolbar();
		}

		if (!isset($this->editorOptions['font_names'])) {
			$this->editorOptions['font_names'] = $this->getFontNames();
		}

		if (!isset($this->editorOptions['removeDialogTabs'])) {
			$this->editorOptions['removeDialogTabs'] = $this->getRemoveDialogTabs();
		}

		if (!isset($this->editorOptions['removeButtons'])) {
			$this->editorOptions['removeButtons'] = $this->getRemoveButtons();
		}

		/*
		if (!isset($this->editorOptions['format_tags'])) {
			$this->editorOptions['format_tags'] = $this->getFormatTags();
		}
		*/

		if (!isset($this->editorOptions['extraPlugins'])) {
			$this->editorOptions['extraPlugins'] = $this->extraPlugins();
		}

		if ($this->allowBrowse) {
			$this->editorOptions['filebrowserBrowseUrl'] = Yii::app()->createAbsoluteUrl('/file/browse');
		}

		if ($this->allowUpload) {
			$params=array();
			if (Yii::app()->getRequest()->enableCsrfValidation) {
				$params[Yii::app()->getRequest()->csrfTokenName] = Yii::app()->getRequest()->getCsrfToken();
			}
			$this->editorOptions['filebrowserUploadUrl'] = Yii::app()->createAbsoluteUrl('/file/upload', $params);
		}
	}

	public function run()
	{
		list($name, $id) = $this->resolveNameID();

		if (isset($_COOKIE['editor_' . $id])) {
			$this->editorOptions['height'] = $_COOKIE['editor_' . $id];
		}

		$this->registerClientScript($id);

		$this->htmlOptions['id'] = $id;

		// Do we have a model?
		if ($this->hasModel()) {
			if ($this->form) {
				if ($this->form instanceof TbActiveForm) {
					$html = $this->form->textAreaRow($this->model, $this->attribute, $this->htmlOptions);
				} else {
					$html = $this->form->textArea($this->model, $this->attribute, $this->htmlOptions);
				}
			} else {
				$html = CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
			}
		} else {
			$html = CHtml::textArea($name, $this->value, $this->htmlOptions);
		}
		echo $html;
	}

	public function getStandardToolbar()
	{
		return array(
			array('name'=>'clipboard', 'groups'=>array('clipboard', 'undo')),
			array('name'=>'editing', 'groups'=>array('find', 'selection', 'spellchecker')),
			array('name'=>'links'),
			array('name'=>'insert'),
			//array('name'=>'forms'),

			array('name'=>'document', 'groups'=>array('mode', 'document', 'doctools')),
			array('name'=>'others'),
			'/',
			array('name'=>'basicstyles', 'groups'=>array('basicstyles', 'cleanup')),
			array('name'=>'paragraph', 'groups'=>array('list', 'indent', 'blocks', 'align', 'bidi')),
			array('name'=>'styles'),
			array('name'=>'colors'),
			array('name'=>'tools'),
			array('name'=>'about'),
		);
	}

	public function getFormatTags()
	{
		return 'p;h1;h2;h3;pre';
	}

	public function extraPlugins()
	{
		return '';
	}

	public function getRemoveButtons()
	{
		return 'CreateDiv,Styles';
	}

	public function getRemoveDialogTabs()
	{
		return 'image:advanced;link:advanced';
	}

	public function getFontNames()
	{
		return
			'宋体/宋体;黑体/黑体;仿宋/仿宋_GB2312;楷体/楷体_GB2312;隶书/隶书;幼圆/幼圆;微软雅黑/微软雅黑;'.
			'Arial/Arial, Helvetica, sans-serif;' .
			'Comic Sans MS/Comic Sans MS, cursive;' .
			'Courier New/Courier New, Courier, monospace;' .
			'Georgia/Georgia, serif;' .
			'Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif;' .
			'Tahoma/Tahoma, Geneva, sans-serif;' .
			'Times New Roman/Times New Roman, Times, serif;' .
			'Trebuchet MS/Trebuchet MS, Helvetica, sans-serif;' .
			'Verdana/Verdana, Geneva, sans-serif';
	}
}